package com.example.algorithm.backtrcaking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author : zhy
 * @since : 2022/8/23
 * 子集
 */
public class Subset {

    List<List<Integer>> list=new ArrayList<>();
    LinkedList<Integer> path=new LinkedList<>();
    public List<List<Integer>> subsets(int[] nums) {
        if(nums.length==0)
        {
            return list;
        }
        backTracking(nums,0);
        return list;
    }

    private void backTracking(int[] nums, int startIndex) {

        list.add(new ArrayList<>(path));
        if(startIndex>=nums.length)
        {
            return;
        }
        for(int i=startIndex;i<nums.length;i++)
        {
            path.add(nums[i]);
            backTracking(nums, i+1);
            path.remove(path.size()-1);
        }

    }

    public static void main(String[] args) {
        Subset subset = new Subset();
        int [] num={1,2,3};
        List<List<Integer>> subsets = subset.subsets(num);
        subsets.stream().forEach(System.out::println);
    }

}
